﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;
using System.Runtime.CompilerServices;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IDataRowCollection))]
    [Guid("0D1F61FD-CE1E-43F6-8CE7-4F4CA295EF65")]
    public class DataRowCollection : IDataRowCollection
    {
        private AdoNet.DataRowCollection _adoNetRowCollection;

        public DataRowCollection() : this(null) { }

        public DataRowCollection(AdoNet.DataRowCollection adoNetRowCollection)
        {
            if (adoNetRowCollection == null)
                throw new ArgumentNullException("adoNetRowCollection");
            else
                _adoNetRowCollection = adoNetRowCollection;
        }

        [ComVisible(false)]
        public AdoNet.DataRowCollection AdoNetDataRowCollection
        {
            get { return _adoNetRowCollection; }
        }

        [DispId(-4)]
        public IEnumerator GetEnumerator()
        {
            var result = from AdoNet.DataRow row in _adoNetRowCollection
                         select new DataRow(row);

            return result.GetEnumerator();
        }

        [DispId(0)]
        [IndexerName("_Default")]
        public DataRow this[int index]
        {
            get
            {
                return new DataRow(_adoNetRowCollection[index]);
            }
        }

        public void Add(DataRow row)
        {
            _adoNetRowCollection.Add(row.AdoNetDataRow);
        }               

        public void Clear()
        {
            _adoNetRowCollection.Clear();
        }

        public bool Contains(object key)
        {
            return _adoNetRowCollection.Contains(key);
        }

        public bool ContainsKeys(ObjectList keys)
        {
            return _adoNetRowCollection.Contains(keys.InternalList.ToArray());
        }

        public int IndexOf(DataRow row)
        {
            return _adoNetRowCollection.IndexOf(row.AdoNetDataRow);
        }

        public void Remove(DataRow row)
        {
            _adoNetRowCollection.Remove(row.AdoNetDataRow);
        }

        public void RemoveAt(int index)
        {
            _adoNetRowCollection.RemoveAt(index);
        }

        public int Count
        {
            get { return _adoNetRowCollection.Count; }
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("57C50A91-A09E-4F65-80FE-48041223ED96")]
    public interface IDataRowCollection
    {
        [DispId(-4)]
        IEnumerator GetEnumerator();

        [DispId(0)]
        [IndexerName("_Default")]
        DataRow this[int index]
        {
            get;
        }

        void Add(DataRow row);

        void Clear();

        bool Contains(object key);

        bool ContainsKeys(ObjectList keys);

        int IndexOf(DataRow row);

        void Remove(DataRow row);

        void RemoveAt(int index);

        int Count
        {
            get;
        }
    }
}
